package com.hnkywl.sts.service.sys;

import com.hnkywl.sts.dao.sys.ZdkhzDao;
import com.hnkywl.sts.entity.sys.Zdkhz;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-20
 * Time: 下午7:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ZdkhzService {

    private ZdkhzDao zdkhzDao;

    @Transactional
    public String findAllZdkhzList(String zdkhzKh, Date start, Date endDate, int pageNo, int pageSize) {
        Page<Zdkhz> zdkhzPage = this.zdkhzDao.findAllZdkhzList(zdkhzKh, start, endDate, pageNo, pageSize);
        List<Zdkhz> zdkhzList = zdkhzPage.getResult() == null ? new ArrayList<Zdkhz>() : zdkhzPage.getResult();
        String zdkhzJson = BaseUtils.fetchCollectionToJson(zdkhzList, "id", "zyf.qc", "zdkhz", "drrq", "dateCreated", "lastUpdated");
        return "{\"total\":" + zdkhzPage.getTotalCount() + ",\"rows\":[" + zdkhzJson + "]}";
    }


    public List<Map> findExportQyjgZdkhz() {
        return this.zdkhzDao.findExportQyjgZdkhz();
    }

    @Transactional
    public void saveOrUpdateZdkhz(List<Zdkhz> khjList) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        for (Zdkhz zdkhz : khjList) {
            String selHql = " from Zdkhz where zyf.id = ? ";
            List<Zdkhz> zList = this.zdkhzDao.find(selHql,zdkhz.getZyf().getId());
            if (null != zList && zList.size() > 0) {
                Zdkhz zdkhzT = zList.get(0);
                zdkhzT.setZdkhz(zdkhz.getZdkhz());
                zdkhzT.setLastUpdated(zdkhzT.getLastUpdated());
                this.zdkhzDao.save(zdkhzT);
            } else {
                this.zdkhzDao.save(zdkhz);
            }
        }
    }


    @Transactional
    public Zdkhz saveZdkhz(Long zdkhzId, String zdkhz) {
        Zdkhz z = zdkhzDao.get(zdkhzId);
        z.setZdkhz(Double.parseDouble(zdkhz));
        z.setLastUpdated(new Date());
        this.zdkhzDao.save(z);
        return z;
    }

    @Autowired
    public void setZdkhzDao(ZdkhzDao zdkhzDao) {
        this.zdkhzDao = zdkhzDao;
    }

}
